package com.example.farmeri.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;

import androidx.constraintlayout.widget.Group;
import com.example.farmeri.R;
import com.example.farmeri.pojo.User;
import com.example.farmeri.presenter.login.ILoginContract;
import com.example.farmeri.presenter.login.LoginPresenter;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.base.BaseActivity;
import com.example.farmeri.ui.main.MainActivity;
import com.github.ybq.android.spinkit.sprite.Sprite;
import com.github.ybq.android.spinkit.style.FoldingCube;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录
 */
public class SignInActivity extends BaseActivity<LoginPresenter> implements ILoginContract{

    //访问身份
    @BindView(R.id.sign_in_name)
    EditText loginName;
    // 年龄
    @BindView(R.id.sign_in_age)
    EditText age;
    //电话
    @BindView(R.id.sign_in_tel)
    EditText tel;
    //密码
    @BindView(R.id.sign_in_pwd)
    EditText pwd;
    //加载框
    @BindView(R.id.sign_in_progress)
    ProgressBar progressBar;

    @BindView(R.id.sign_up_group)
    Group viewGroup;

    @BindViews({R.id.sign_in_name,
            R.id.sign_in_age,
            R.id.sign_in_tel,
            R.id.sign_in_pwd})
    List<EditText> formList;

    //选择身份
    @BindView(R.id.sign_in_spinner)
    Spinner spinner;

    //身份
    List<String > list;

    private User user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        ButterKnife.bind(this);
        initProgressBar();
        spinnerAddData();
        present = new LoginPresenter(this);
    }


    private void initProgressBar() {
        Sprite foldingCube = new FoldingCube();
        progressBar.setIndeterminateDrawable(foldingCube);
    }

    @OnClick(R.id.sign_in_button)
    void signIn(){
        if (!present.checkData(this,formList)){
            return;
        }
        //填充注册表单的内容
        setForm();
        viewGroup.setVisibility(View.VISIBLE);
        present.login(user);
    }

    /**成都大学 湿度
     * 设置表单内容
     */
    private void setForm() {
        user = new User();
        user.setLogin_name(loginName.getText().toString());
        //registerModel.setCode(edit_checkNum.getText().toString());
        user.setTel(tel.getText().toString());
        user.setAge(age.getText().toString());
        user.setPassword(pwd.getText().toString());
        Log.d(TAG, "setForm: User = "+user.toString());
    }



    private void spinnerAddData() {
        getRole();
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    private void getRole() {
        list = new ArrayList<>();
        list.add("开发管理员");
        list.add("温度管理员");
        list.add("湿度管理员");
        list.add("光照管理员");
    }


    @Override
    public void onLoginSuccess(ResponseUtils<User> responseUtils) {
        viewGroup.setVisibility(View.GONE);
        if (responseUtils.getCode() == 200){
            Log.d("response", "onMessageEvent: response = " + responseUtils.toString());
            present.saveUserInfo(this, responseUtils.getData());
            startActivity(new Intent(this, MainActivity.class));
        }else{
            showToast(responseUtils.getMessage());
        }
    }

    @Override
    public void showMessage(String msg) {
        if (msg == null){
            showToast("登录错误");
            viewGroup.setVisibility(View.GONE);
        }
        showToast(msg);
    }
}
